package com.jtjrenren.android.taxi.passenger.core.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jtjrenren.android.taxi.passenger.Constants;
import com.jtjrenren.android.taxi.passenger.R;
import com.jtjrenren.android.taxi.passenger.bean.api.ApiCouList;
import com.jtjrenren.android.taxi.passenger.base.BaseRcyAdapter;

/**
 * Created by Administrator on 2016/1/28.
 */
public class CouponsListAdapter extends BaseRcyAdapter<ApiCouList.DataListBean> {
    public interface ItemClickListener {
        void itemClick(boolean isHeader);
    }

    private boolean isChoose;
    private ApiCouList.DataListBean selectedCoupons;
    private ItemClickListener itemClickListener;

    public CouponsListAdapter(Context context, boolean isChoose) {
        super(context);
        this.isChoose = isChoose;
        if (isChoose)
            mode = MODE_HEADER;
    }

    public void setSelectedCoupons(ApiCouList.DataListBean selectedCoupons) {
        this.selectedCoupons = selectedCoupons;
    }

    public ApiCouList.DataListBean getSelectedCoupons() {
        return selectedCoupons;
    }

    public void setItemClickListener(ItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    @Override
    public RecyclerView.ViewHolder getItemViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_coupons_list, parent, false);

        return new CouponsViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder getHeaderViewHolder(ViewGroup parent) {
        View view = inflater.inflate(R.layout.header_coupon_nouse, parent, false);
        return new HeaderViewHolder(view);
    }

    @Override
    public void onBindItemViewHolder(RecyclerView.ViewHolder holder, int position) {
        CouponsViewHolder couponsViewHolder = (CouponsViewHolder) holder;
        if (isChoose) {
            couponsViewHolder.cb_choose.setVisibility(View.VISIBLE);
        } else couponsViewHolder.cb_choose.setVisibility(View.GONE);

        ApiCouList.DataListBean info = list.get(position);
        couponsViewHolder.commentTv.setText(info.getComment());
        couponsViewHolder.validityDateTv.setText(info.getValidityDate());
        couponsViewHolder.titleTv.setText(info.getTitle());
        couponsViewHolder.couponsPriceTv.setText(info.getDenomination() + "");
        couponsViewHolder.ll_bg.setBackgroundResource(R.mipmap.def_coupons_use_normal);

        int status = info.getStatus();
        switch (status) {
            case Constants.DEFAULT_COUPONS_STATUS_NOTUSE:
                setCommon(couponsViewHolder, true);
                couponsViewHolder.ll_bg.setBackgroundResource(R.mipmap.def_coupons_use);
                break;
            case Constants.DEFAULT_COUPONS_STATUS_OVERDATE:
                setCommon(couponsViewHolder, false);
                couponsViewHolder.ll_bg.setBackgroundResource(R.mipmap.def_coupons_overdate);
                break;
            case Constants.DEFAULT_COUPONS_STATUS_USED:
                setCommon(couponsViewHolder, false);
                couponsViewHolder.ll_bg.setBackgroundResource(R.mipmap.def_coupons_used);
                break;
            case Constants.DEFAULT_COUPONS_STATUS_ZUOFEI:
            case Constants.DEFAULT_COUPONS_STATUS_OTHER:
                setCommon(couponsViewHolder, false);
                break;
        }

        if (selectedCoupons != null && info.getID().equals(selectedCoupons.getID())) {
            couponsViewHolder.cb_choose.setChecked(true);
        } else {
            couponsViewHolder.cb_choose.setChecked(false);
        }


    }

    private void setCommon(CouponsViewHolder holder, boolean emp) {
        int colorId = R.color.gray_white;
        if (emp)
            colorId = R.color.label_green;

        holder.priceUnitTv.setTextColor(context.getResources().getColor(colorId));
        holder.validityDateTv.setTextColor(context.getResources().getColor(colorId));
        holder.commentTv.setTextColor(context.getResources().getColor(colorId));
        holder.couponsPriceTv.setTextColor(context.getResources().getColor(colorId));
        holder.titleTv.setTextColor(context.getResources().getColor(colorId));
    }

    public class CouponsViewHolder extends RecyclerView.ViewHolder {
        TextView commentTv, validityDateTv, titleTv, couponsPriceTv,
                priceUnitTv;
        CheckBox cb_choose;
        LinearLayout ll_bg;

        public CouponsViewHolder(View itemView) {
            super(itemView);

            initViews();

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = getItemPosition(getAdapterPosition());
                    cb_choose.setChecked(true);
                    selectedCoupons = list.get(position);
                    if (isChoose && itemClickListener != null)
                        itemClickListener.itemClick(false);
                }
            });

        }

        private void initViews() {
            commentTv = (TextView) itemView.findViewById(R.id.item_coupons_comment);
            validityDateTv = (TextView) itemView.findViewById(R.id.item_coupons_date);
            titleTv = (TextView) itemView.findViewById(R.id.item_coupons_title);
            couponsPriceTv = (TextView) itemView.findViewById(R.id.item_coupons_price);
            priceUnitTv = (TextView) itemView.findViewById(R.id.item_coupons_unit);

            ll_bg = (LinearLayout) itemView.findViewById(R.id.item_coupons_bg);
            cb_choose = (CheckBox) itemView.findViewById(R.id.item_coupons_choose);
        }
    }

    public class HeaderViewHolder extends RecyclerView.ViewHolder {

        public HeaderViewHolder(View itemView) {
            super(itemView);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    selectedCoupons = null;
                    if (isChoose && itemClickListener != null)
                        itemClickListener.itemClick(true);
                }
            });
        }
    }


}
